package mvv.learn.spring.utilities;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * @author Manh Vu
 */
@Component
public class JsonUtils {
	private static final Logger log = LoggerFactory.getLogger(JsonUtils.class);

	private static ObjectMapper objectMapper;

	public static String getPrettyJson(Object obj) {
		String json = "";

		try {
			json = objectMapper.writerWithDefaultPrettyPrinter().writeValueAsString(obj);
		}catch (JsonProcessingException e) {
			log.error(e.getMessage(), e);
		}

		return json;
	}

	public static ObjectMapper getObjectMapper() {
		return objectMapper;
	}

	@Autowired
	public void setObjectMapper(ObjectMapper objectMapper) {
		JsonUtils.objectMapper = objectMapper;
	}
}
